package cn.ds.assets.module.assets.service.projectMember;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.projectMember.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.projectMember.BaseProjectMemberDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.projectMember.BaseProjectMemberConvert;
import cn.ds.assets.module.assets.dal.mysql.projectMember.BaseProjectMemberMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 项目成员 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseProjectMemberServiceImpl implements BaseProjectMemberService {

    @Resource
    private BaseProjectMemberMapper baseProjectMemberMapper;

    @Override
    public Long createBaseProjectMember(BaseProjectMemberCreateReqVO createReqVO) {
        // 插入
        BaseProjectMemberDO baseProjectMember = BaseProjectMemberConvert.INSTANCE.convert(createReqVO);
        baseProjectMemberMapper.insert(baseProjectMember);
        // 返回
        return baseProjectMember.getId();
    }

    @Override
    public void updateBaseProjectMember(BaseProjectMemberUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseProjectMemberExists(updateReqVO.getId());
        // 更新
        BaseProjectMemberDO updateObj = BaseProjectMemberConvert.INSTANCE.convert(updateReqVO);
        baseProjectMemberMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseProjectMember(Long id) {
        // 校验是否存在
        validateBaseProjectMemberExists(id);
        // 逻辑删除
        baseProjectMemberMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseProjectMemberExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseProjectMemberDO dataDo = baseProjectMemberMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseProjectMemberDO getBaseProjectMember(Long id) {
        return baseProjectMemberMapper.selectById(id);
    }

    @Override
    public List<BaseProjectMemberDO> getBaseProjectMemberList(Collection<Long> ids) {
        return baseProjectMemberMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseProjectMemberDO> getBaseProjectMemberPage(BaseProjectMemberPageReqVO pageReqVO) {
        return baseProjectMemberMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseProjectMemberDO> getBaseProjectMemberList(BaseProjectMemberExportReqVO exportReqVO) {
        return baseProjectMemberMapper.selectList(exportReqVO);
    }

}
